layui.use(['table', 'admin', 'ax', 'func','laydate','formSelects', 'form', 'qrcode'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    /**
     * 运营组-设备管理
     */
    var NanSellerDevice = {
        tableId: "nanSellerDeviceTable"
    };


    /**
     * 初始化表格的列
     */
    NanSellerDevice.initColumn = function () {
        return [[
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 120},
            {field: 'tagStr', sort: false, title: '标签', width: 120},
            {field: 'address', sort: false, title: '地址头', width: 120},
            {field: 'sellerId', sort: false, title: '运营组ID'},
            {field: 'sellerName', sort: false, title: '运营组'},
            {field: 'cate', sort: false, title: '设备分类'},
            {field: 'img', sort: false, title: '设备图片', templet: '<div><img src="{{Feng.cdn}}{{d.img}}" alt="" /></div>'},
            {field: 'model', sort: false, title: '设备名称'},
            {title: '二维码', width: 130},
            {field: 'code', sort: false, title: '编码'},
            {field: 'num', sort: false, title: '绑定人数'},
            {field: 'isForbid', sort: false, title: '是否禁用', templet: '<div>{{d.isForbid? "是":"否"}}</div>'},
            {field: 'createTime', sort: false, title: '创建时间'},
            {field: 'updateTime', sort: false, title: '更新时间'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    NanSellerDevice.search = function () {
        var queryData = {};

        queryData['sellerId'] = $('#sellerId').val();
        queryData['code'] = $('#code').val();
        queryData['isForbid'] = $('#isForbid').val();

        table.reload(NanSellerDevice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    NanSellerDevice.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/nanSellerDevice/add'
    };

    /**
    * 跳转到编辑页面
    *
    * @param data 点击按钮时候的行数据
    */
    NanSellerDevice.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/nanSellerDevice/edit?id=' + data.id
    };

    /**
     * 导出excel按钮
     */
    NanSellerDevice.exportExcel = function () {
        var checkRows = table.checkStatus(NanSellerDevice.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    NanSellerDevice.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/nanSellerDevice/delete", function (data) {
                Feng.success("操作成功!");
                table.reload(NanSellerDevice.tableId);
            }, function (data) {
                Feng.error("操作失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否确定", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + NanSellerDevice.tableId,
        url: Feng.ctxPath + '/nanSellerDevice/list',
        page: true,
        limit: 20,
        height: "full-158",
        cellMinWidth: 100,
        cols: NanSellerDevice.initColumn(),
        done: function (res, curr, count) {
            tableList = res.data;
            var that = this.elem.next()
            res.data.forEach(function (item, index) {
                var tr = that.find(".layui-table-box tbody tr[data-index=" + index + "]")
                var td = tr.find('.laytable-cell-1-0-8')//需要展示二维码的列，从0开始
                $(td).css("height", "100px").qrcode({
                    render: "canvas",
                    width: 100,
                    height: 100,
                    foreground: "#000",
                    background: "#fff",
                    text: item.code
                })
            })
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        NanSellerDevice.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    NanSellerDevice.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        NanSellerDevice.exportExcel();
    });

    table.on('sort('+NanSellerDevice.tableId+')', function(obj){
            table.reload(NanSellerDevice.tableId, {
                initSort: obj,
                where: {
                    field: obj.field.replace(/([A-Z])/g,"_$1").toLowerCase()
                    ,order: obj.type
                }
            });
        });

    // 工具条点击事件
    table.on('tool(' + NanSellerDevice.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            NanSellerDevice.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            NanSellerDevice.onDeleteItem(data);
        }
    });

});
